package cn.jiyun.controller;


import cn.jiyun.common.config.Result;
import cn.jiyun.entity.XmAudit;
import cn.jiyun.entity.XmUser;
import cn.jiyun.service.IXmAuditService;
import cn.jiyun.service.IXmUserService;
import com.aliyun.oss.OSS;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.lang.reflect.Type;
import java.time.LocalDateTime;
import java.util.Base64;
import java.util.Random;
import java.util.UUID;

import static com.google.common.io.Files.getFileExtension;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-11-21
 */
@RestController
@RequestMapping("/xm-client/xm-audit")
public class XmAuditController {

    @Value("${aliyun.bucketName}")
    private String buncket;
    @Value("${aliyun.endpoint}")
    private String endpoint;
    @Value("${aliyun.keyId}")
    private String accessKeyId;
    @Value("${aliyun.keySecret}")
    private String accessKeySecret;
    @Autowired
    private IXmAuditService xmAuditService;

    @RequestMapping("findAll")
    public Result findAll(Integer current, Integer pageSize, @RequestBody XmAudit xmAudit){
        Page<XmAudit> xmAuditPage=xmAuditService.findAll(current,pageSize,xmAudit);
        return Result.ok(xmAuditPage);
    }


//    修改审核状态
    @RequestMapping("updateAudit")
    public Result updateAudit(@RequestBody XmAudit xmAudit){
        xmAuditService.xmAudit(xmAudit);
        return Result.ok();
    }


//    根据id查询
    @RequestMapping("findById")
    public Result findById(Integer id){
        XmAudit xmAudit=xmAuditService.getById(id);
        return Result.ok(xmAudit);
    }



//    添加审核用户要审核的信息
    @RequestMapping("addAudit")
    public Result addAudit(@RequestBody XmAudit xmAudit){
        System.out.println(xmAudit+"___________");
        xmAudit.setSubmissionTime(LocalDateTime.now());
        xmAudit.setSerialNumber(new Random().nextLong()+"");
        xmAudit.setStatus(1);
        xmAuditService.save(xmAudit);
        return Result.ok();
    }

    @Autowired
    private OSS oss;


//    将图片存放到阿里云
    @RequestMapping("upload")
    public String upload(MultipartFile file,HttpServletRequest request){
        String originalFilename = file.getOriginalFilename();
        // 生成唯一文件名，避免重名
        String fileName = UUID.randomUUID().toString() + "." + getFileExtension(originalFilename);
        // 获取阿里云OSS相关配置（假设你已经有正确配置好的方式来获取这些，比如通过配置类注入）

        try {
            // 上传文件到阿里云OSS
            oss.putObject(buncket, fileName, file.getInputStream());
//            获取图片地址
            String url = "https://" + buncket + "." + endpoint + "/" + fileName;
            return url;
        } catch (Exception e) {
            e.printStackTrace();
            return "图片上传失败";
        }
    }






}
